package com.datastructure.test.findnumsappearonce;

import java.util.ArrayList;
import java.util.Arrays;

public class FindNumsAppearOnce {

    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        if(array.length<=2){
            return array;
        }
        Arrays.sort(array);
        ArrayList<Integer> data = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            if(i==0&&array[0]!=array[1]){
                data.add(array[0]);
            }
            if(i>0&&array[i]==array[i-1]){
                continue;
            }
            if(i+1<= array.length-1) {
                if (i > 0 && array[i] != array[i - 1] && array[i] != array[i + 1]) {
                    data.add(array[i]);
                }
            }
            if(i== array.length-1&&array[array.length-1]!=array[array.length-2]){
                data.add(array[array.length-1]);
            }
        }
        int [] result = new int[data.size()];
        for (int i = 0; i < result.length; i++) {
            result[i]= data.get(i);
        }
        return result;
    }
}
